//
//  PurseViewController.m
//  Xing
//
//  Created by 徐番茄丶 on 16/1/4.
//  Copyright © 2016年 徐番茄丶. All rights reserved.
//

#import "PurseViewController.h"
#import "MyTools.h"
#import "PurseTableViewCell.h"
#import "GetMoneyViewController.h"
#import "ListViewController.h"

@interface PurseViewController ()<
UITableViewDataSource,
UITableViewDelegate,
UIActionSheetDelegate>{
    UITableView *_myTab;
    NSDictionary *_mydic;
}

@end

@implementation PurseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的钱包";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStylePlain target:self action:@selector(list)];
    [self.navigationItem.rightBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    [self viewCreate];
}

-(void)viewCreate{
    
    
    
    _myTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _myTab.rowHeight = 50;
    _myTab.backgroundColor = UIColorFromRGB(0xefeff1);
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
    _myTab.scrollEnabled = NO;
    _myTab.delegate = self;
    _myTab.dataSource = self;
    [self.view addSubview:_myTab];
    
}

-(void)getDate{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:CASHDES_URL parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _mydic = [[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"] ];
            
            [_myTab reloadData];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark tableview代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    PurseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[PurseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    if (indexPath.row == 0) {
        cell.myImage.image = [UIImage imageNamed:@"余额"];
        cell.titleLab.text = @"我的余额";
        if ([_mydic objectForKey:@"账户资金"]) {
            cell.moneyLab.text = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"账户资金"]];
        }else{
            cell.moneyLab.text = @"0";
        }
        cell.moneyLab.textColor = UIColorFromRGB(0x999999);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 49, WIDTH, 1)];
        footview.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [cell addSubview:footview];
    }else{
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH  - 100, 10, 80, 30)];
        btn.layer.cornerRadius = 3;
        btn.backgroundColor = UIColorFromRGB(0x9CCC66);
        [btn setTitle:@"提现" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(getJifen) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@"提现到" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"支付宝"/*,@"微信"*/, nil];
    //    [action showInView:self.view];
    [self getJifen];
}

-(void)getJifen{
    GetMoneyViewController *get = [[GetMoneyViewController alloc]init];
    get.isHow = @"支付宝";
    [self.navigationController pushViewController:get animated:YES];
}

#pragma mark sheet代理--选择提现的账户


//-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//    if (buttonIndex == 0) {
//        //支付宝
//        GetMoneyViewController *get = [[GetMoneyViewController alloc]init];
//        get.isHow = @"支付宝";
//        [self.navigationController pushViewController:get animated:YES];
//    }

//    }else if (buttonIndex == 1){
//        //微信
//        GetMoneyViewController *get = [[GetMoneyViewController alloc]init];
//        get.isHow = @"微信";
//        [self.navigationController pushViewController:get animated:YES];
//    }
//}

-(void)list{
    ListViewController *lise = [[ListViewController alloc]init];
    [self.navigationController pushViewController:lise animated:YES];
}


-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    [self getDate];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //    self.tabBarController.tabBar.translucent = NO;
    //    self.tabBarController.tabBar.hidden = NO;
}


@end
